package video.receiver
{
	import ctimer.CTimer;
	import ctimer.Task;
	
	import flash.text.ReturnKeyLabel;
	
//	import httpserver.CameraHttpServer;
	
	import mx.controls.Alert;
	
	import p2pserver.P2pNetStream;
	
	import video.VideoPlayer;

	[Bindable]
	public class DeviceManager
	{
		public var currentTime:String;
		public var bufferTime:uint = 3;
		
		private var _videoDevice:Object = null;
		private var _netStream:P2pNetStream = null;
//		private var httpServer:CameraHttpServer = null;
		private var _videoPlayer:VideoPlayer = null;
		private var _timer:CTimer = null;
		private var videoPlaytimeTask:Task = null;
		
		public function DeviceManager(){
			
			videoPlaytimeTask = new Task(RefreshPlayTime, 1, true);
		}

		public function get timer():CTimer{
			return _timer;
		}

		public function set timer(value:CTimer):void{
			_timer = value;
		}

		public function get videoPlayer():VideoPlayer{
			return _videoPlayer;
		}

		public function set videoPlayer(value:VideoPlayer):void{
			_videoPlayer = value;
		}

		public function get netStream():P2pNetStream{
			return _netStream;
		}

		public function set netStream(value:P2pNetStream):void{
			_netStream = value;
		}

		public function get videoDevice():Object{
			return _videoDevice;
		}

		public function set videoDevice(value:Object):void{
			_videoDevice = value;
		}
		
		public function VerifyDevice(url:String, jsonStr:String, verifySuccess:Function):void{
			
//			httpServer = new CameraHttpServer(url, jsonStr, verifySuccess, timer);
//			httpServer.sendRequest();
		}
		
		public function PlayDevice():void{
			
			if (_netStream == null){
				return;
			}

			_netStream.bufferTime = bufferTime;
			
			_netStream.play(P2pNetStream.STREAM_NAME);	
			videoPlayer.recVideo.attachNetStream(_netStream);
			
			timer.AddTask(videoPlaytimeTask);
		}
		
		public function StopDevice():void{
			
			if (_netStream == null){
				return;
			}
			
			videoPlayer.recVideo.clear();
			videoPlayer.videoState = "无图像";
			
			timer.DeleteTask(videoPlaytimeTask);
			
			if (_netStream != null){
				_netStream.pause();
			}
		}
		
		private function RefreshPlayTime():void{
			if (_netStream != null){
				currentTime = TimeConvert(_netStream.time);
			}
			//if (stream.bufferLength < stream.bufferTime){
			if (_netStream.bufferLength <= 0){
				videoPlayer.videoState = "无图像";
				
			}else{
				videoPlayer.videoState = "";
			}
			
		}
		
		private static function TimeConvert(time:Number) : String
		{
			if(time < 0){
				return "00:00:00";
			}
			
			var minute:int = 0;
			var hour:* = Math.floor(time / 3600);
			
			time = time - 3600 * hour;
			if (time > 0){
				minute = Math.floor(time / 60);
			}else{
				minute = 0;
			}
			var second:* = time - 60 * minute;
			var timeFormateStr:* = AppendZero(hour) + ":" + AppendZero(minute) + ":" + AppendZero(second);
			
			return timeFormateStr;
		}
		
		private static function AppendZero(time:int) : String
		{
			var timeStr:* = String(time);
			if (timeStr.length == 1){
				timeStr = "0" + timeStr;
			}
			return timeStr;
		}

	}
}